using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using MemberMgr.Data.ManagerObjects;
using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.Base;

namespace MemberMgr.Data.UnitTests
{
	[TestFixture]
    public partial class tUserTests : UNuitTestBase
    {
        protected ItUserManager manager;
		
		public tUserTests()
        {
            manager = managerFactory.GettUserManager();
        }
		
		protected tUser CreateNewtUser()
		{
			tUser entity = new tUser();
			
			
			entity.Userid = "Test";
			entity.Password = "Test Test ";
			entity.Username = "Test Test ";
			entity.Email = "Test Test ";
			
			ItShopManager tShopManager = managerFactory.GettShopManager();
			entity.TShop = tShopManager.GetAll(1)[0];
			
			ItRoleManager tRoleManager = managerFactory.GettRoleManager();
			entity.TRole = tRoleManager.GetAll(1)[0];
			
			return entity;
		}
		protected tUser GetFirsttUser()
        {
            IList<tUser> entityList = manager.GetAll(1);
            if (entityList.Count == 0)
                Assert.Fail("All tables must have at least one row for unit tests to succeed.");
            return entityList[0];
        }
		
		[Test]
        public void Create()
        {
            try
            {
				tUser entity = CreateNewtUser();
				
                object result = manager.Save(entity);

                Assert.IsNotNull(result);
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Read()
        {
            try
            {
                tUser entityA = CreateNewtUser();
				manager.Save(entityA);

                tUser entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA, entityB);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
		[Test]
		public void Update()
        {
            try
            {
                tUser entityA = GetFirsttUser();
				
				entityA.Userid = "T";
				
				manager.Update(entityA);

                tUser entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA.Userid, entityB.Userid);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Delete()
        {
            try
            {
                tUser entity = GetFirsttUser();
				
                manager.Delete(entity);

                entity = manager.GetById(entity.Id);
                Assert.IsNull(entity);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
	}
}

